package com.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pojo.Ordersetting;
import com.result.Result;
import com.service.OrdersettingService;
import com.utils.DateUtils;
import com.utils.POIUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLClientInfoException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;


    @RequestMapping("/selectByDate")
    public Result selectByDate() {
        try {
            List<Ordersetting> ordersettings = ordersettingService.selectByDate();
            List<String> strings = new ArrayList<>();
            ordersettings.forEach(o -> {
                try {
                    String s = DateUtils.parseDate2String(o.getOrderdate());
                    strings.add(s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            return new Result(true, MessageConstant.ORDERSETTIN_SUCCESS, strings);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ORDERSETTIN_FAIL);
    }


    @RequestMapping("/queryByDate")
    public Result queryByDate(@RequestBody Date orderDate) {
        try {
            Ordersetting ordersetting = ordersettingService.queryByDate(orderDate);
            return new Result(true, MessageConstant.ORDERSETTIN_SUCCESS, ordersetting);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ORDERSETTIN_FAIL);
    }

    @PostMapping("/setOrderSettingByDate")
    public Result setOrderSettingByDate(Integer number, Date orderDate) throws CustomException {
        //1.存到OrderSetting中
        Ordersetting os = new Ordersetting();
        os.setNumber(number);
        os.setOrderdate(orderDate);
        ordersettingService.setOrderSettingByDate(os);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }


    @PostMapping("/getOrderSettingByDate")
    public Result getOrderSettingByDate(Integer year, Integer month) {
        try {
            List<Map> maps = ordersettingService.getOrderSettingByDate(year, month);
            if (maps != null && maps.size() > 0) {//携带数据
                return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, maps);
            } else {//暂无预约设置数据
                return new Result(true, MessageConstant.GET_ORDERSETTING_ISNULL);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);

    }


    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws CustomException {
        try {
            if (excelFile!=null && !excelFile.isEmpty()){
                //1.读取excel文件
                List<String[]> list = POIUtils.readExcel(excelFile);
                //2.将List<String[]>转成List<OrderSetting>
                List<Ordersetting> orderSettings=new ArrayList<>();
                for (String [] str:list){
                    Ordersetting os=new Ordersetting();
                    os.setOrderdate(new Date(str[0]));
                    os.setNumber(Integer.parseInt(str[1]));
                    orderSettings.add(os);
                }
                //3.添加到数据库中
                ordersettingService.addBatch(orderSettings);
                return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }







}
